<?php
class Covers extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
			$data['covers'] = new cover();
			if(@$_GET['search'])$data['covers']->where("title like '%".$_GET['search']."%'");
			$data['covers']->order_by('id','desc')->get_page(10);
			$this->template->append_metadata(js_checkbox('approve'));
			$this->template->build('admin/cover_index',$data);
	}
	
	function form($id=FALSE)
	{
		$data['cover'] = new cover($id);
		$this->template->append_metadata(js_datepicker());
		$this->template->build('admin/cover_form',$data);
	}
	
	function save($id=FALSE)
	{
		if($_POST)
		{
			$cover = new cover($id);
			
			if(isset($_POST['title']))$_POST['title'] = lang_encode($_POST['title']);
			//$_POST['intro'] = lang_encode($_POST['intro']);
			
			if(isset($_POST['start_date']))$_POST['start_date'] = Date2DB($_POST['start_date']);
			if(isset($_POST['end_date']))$_POST['end_date'] = Date2DB($_POST['end_date']);
			if(!$id)$_POST['user_id'] = $this->session->userdata('id');
			if(@$_FILES['image']['name'])
			{
				if($id)
				{
					$cover->delete_file('uploads/cover','image');
				}
				$cover->image = $cover->upload($_FILES['image'],'uploads/cover');
			}
			$cover->from_array($_POST);
			$cover->save();
			set_notify('success', lang('save_data_complete'));
			redirect('covers/admin/covers/index/'.$cover->category_id);
		}
		else
		{
			redirect($_SERVER['HTTP_REFERER']);
		}
		
	}
	
	function approve($id)
	{
		if($_POST)
		{
			$cover = new cover($id);
			$_POST['approve_id'] = $this->session->userdata('id');
			$cover->approve_date = date("Y-m-d H:i:s");
			$cover->from_array($_POST);
			$cover->save();
		}

	}
	
	function delete($id=FALSE)
	{
		if($id)
		{
			$cover = new cover($id);
			$cover->delete();
			set_notify('success', lang('delete_data_complete'));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	
	
	function delete_file($id)
	{
		$cover = new cover($id);
		$cover->delete_file('uploads/cover/download','file');
		$cover->file = '';
		$cover->save();
		set_notify('success', 'ลบไฟล์เรียบร้อยแล้วค่ะ');
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function remove_image()
	{
		$user = new User($this->session->userdata('id'));
		$user->profile->delete_file($user->profile->id,'uploads/users/','avatar');
		$user->profile->avatar = NULL;
		$user->profile->save();
		set_notify('success', lang('remove_image_complete'));
		redirect('users/admin/profiles');	
	}
	
	function stick_thread($id)
	{
		$data = new Information($id);
		$data->stick = 1;
		$data->save();
		set_notify('success', 'ปักหมุดกระทู้เรียบร้อย');
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function unstick_thread($id)
	{
		$data = new Information($id);
		$data->stick = 0;
		$data->save();
		set_notify('success', 'ยกเลิกการปักหมุดกระทู้เรียบร้อย');
		redirect($_SERVER['HTTP_REFERER']);
	}

}
?>